package io.lckgroup.modules.lck.controller;

import java.util.*;

import io.lckgroup.modules.lck.entity.MerchantUserEntity;
import io.lckgroup.modules.lck.entity.ShopFoodSpecEntity;
import io.lckgroup.modules.lck.form.ShopMerchantUserForm;
import io.lckgroup.modules.lck.service.MerchantUserService;
import io.lckgroup.modules.lck.service.ShopFoodSpecService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lckgroup.modules.lck.entity.ShopMerchantInfoEntity;
import io.lckgroup.modules.lck.service.ShopMerchantInfoService;
import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.R;


/**
 * 商家信息表
 *
 * @author srq
 * @email siruiqi@siruiqi.com
 * @date 2020-01-19 19:14:38
 */
@RestController
@RequestMapping("diancan/shopmerchantinfo")
public class ShopMerchantInfoController {
    @Autowired
    private ShopMerchantInfoService shopMerchantInfoService;
    @Autowired
    private MerchantUserService merchantUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("diancan:shopmerchantinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = shopMerchantInfoService.queryPageByShopMerchantInfoEntity(params);
        return R.ok().put("page", page);
    }
    /**
     *
     * @param params
     * @return 按月份查询商家数量
     */
    @RequestMapping("/merchantNumByMonth")
    @RequiresPermissions("diancan:shopmerchantinfo:list")
    public R merchantNumByMonth(@RequestParam Map<String, Object> params){
        Long[] num =shopMerchantInfoService.selectMerchantInfoByMonth(params);
        return R.ok().put("count", num);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("diancan:shopmerchantinfo:info")
    public R info(@PathVariable("id") Long id) {
        ShopMerchantInfoEntity shopMerchantInfo = shopMerchantInfoService.getById(id);
        return R.ok().put("shopMerchantInfo", shopMerchantInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("diancan:shopmerchantinfo:save")
    public R save(@RequestBody ShopMerchantUserForm ShopMerchantUser) {
        //保存商家信息和用户
        shopMerchantInfoService.saveUser(ShopMerchantUser);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("diancan:shopmerchantinfo:update")
    @CacheEvict(value = {"queryPageByShopMerchantInfoCache", "getMerchantinfo"}, allEntries = true)
    public R update(@RequestBody ShopMerchantInfoEntity shopMerchantInfo) {
        shopMerchantInfo.setGmtModified(new Date());
        shopMerchantInfoService.updateById(shopMerchantInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("diancan:shopmerchantinfo:delete")
    @CacheEvict(value = {"queryPageByShopMerchantInfoCache", "getMerchantinfo"}, allEntries = true)
    public R delete(@RequestBody Long[] ids) {
        shopMerchantInfoService.BatchDelete(ids);
        return R.ok();
    }


    /**
     * 重置密码
     */
    @RequestMapping("/resetPassword")
    @RequiresPermissions("diancan:shopmerchantinfo:update")
    public R resetPassword(@RequestParam Map<String, Object> params) {
        merchantUserService.resetPassword(Long.parseLong(params.get("userId").toString()));
        return R.ok();
    }


}
